import mainRoot from "../../../Game/MainRoot";
import O1HD from "../../../engines/gameEngine";
import level_peopleArchery from "../level_peopleArchery";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_peopleArchery_openBoxPage extends cc.Component {

    @property({type:cc.Node,tooltip:"itemcontent"})
    itemContent:cc.Node = null;

    @property({type:cc.Node,tooltip:"item"})
    item:cc.Node = null;

    @property({type:cc.Label,tooltip:"按钮文本"})
    lbBtn:cc.Label = null;

    level:level_peopleArchery = null;

    datas:any[] = [];

    curSelectItem:cc.Node = null;

    completeFun:()=>void = null;

    bindData(datas:any[],level:level_peopleArchery,completeFun?:()=>void){
        this.datas = datas;
        console.log(datas,">>>>>>>>")
        this.level = level;
        this.completeFun = completeFun;
        this.refrenshPage();
    }

    onSelectOne(e:cc.Event.EventTouch){
        this.itemContent.children.forEach((n:cc.Node)=>{
            n.getChildByName("select").active = false;
        });
        this.curSelectItem = e.target;
        this.curSelectItem.getChildByName("select").active = true;
    }

    refrenshPage(){
        this.lbBtn.string = "双倍获取";
        for(let i = 0;i<this.datas.length;i++){
            let item:cc.Node = cc.instantiate(this.item);
            let icon:cc.Sprite = item.getChildByName("icon").getComponent(cc.Sprite);
            mainRoot.LevelSystem.getOneTypeBundle(this.level.getLevelInfo().levelType).load(`dialog/${this.datas[i].param_type == "6" ?"gong":"xue"}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
                if(err){
                    console.log("加载图片失败");
                    return;
                }
                icon.spriteFrame = spf;
            });
            let count:number = Number(this.datas[i].option_param);
            let optionText:string = O1HD.getManagers().peopleArcheryManager.parseDataAsOptionText(this.datas[i]);
            let lbParam:cc.Label = item.getChildByName("layout").getChildByName("lbDesc").getComponent(cc.Label);
            let lbCount:cc.Label = item.getChildByName("layout").getChildByName("lbCount").getComponent(cc.Label);
            lbParam.string = optionText + ":";
            console.log(optionText,"?????")
            lbCount.string = count.toString();
            item.setParent(this.itemContent);
            item.on('touchend',this.onSelectOne,this);
        }
        if(this.datas.length <=1){
            this.curSelectItem = this.itemContent.children[0];
            this.curSelectItem.getChildByName("select").active = true;
        }
    }

    update(dt: number): void {
        this.level.gamePauseFlag = true;
    }

    onBtnComit(){
        if(!this.curSelectItem){
            O1HD.getModules().uiControlModule.showMessageBox("请选择奖励");
        }else{
            this.closePage();
        }
    }

    onBtnGetBullet(){
        if(!this.curSelectItem){
            O1HD.getModules().uiControlModule.showMessageBox("请选择奖励");
        }else{
            if(O1HD.getServices().userSrv.getCoins() > 0){
                O1HD.getServices().userSrv.setCoins(-1,true);
                // if(this.completeFun){ 
                //     for(let i = 0;i<this.datas.length;i++){
                //         this.level.parseDataAddPeople(this.datas[i]);
                //     }
                // }else{
                //     this.getBullet();
                // }
                let silbilingIndex = this.curSelectItem.getSiblingIndex();
                let data = this.datas[silbilingIndex];
                this.level.parseDataAddPeople(data);
                this.closePage();
            }else{
                O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                    // if(this.completeFun){ 
                    //     for(let i = 0;i<this.datas.length;i++){
                    //         this.level.parseDataAddPeople(this.datas[i]);
                    //     }
                    // }else{
                    //     this.getBullet();
                    // }
                    let silbilingIndex = this.curSelectItem.getSiblingIndex();
                    let data = this.datas[silbilingIndex];
                    this.level.parseDataAddPeople(data);
                    this.closePage();
                },()=>{
    
                },null,"看广告获得子弹");
            }
        }
    }

    getBullet(){
        this.level.parseDataAddPeople({param_type:"4",option_param:1});
    }

    closePage(){
        // for(let i = 0;i<this.datas.length;i++){
        //     this.level.parseDataAddPeople(this.datas[i]);
        // }
        let silbilingIndex = this.curSelectItem.getSiblingIndex();
        let data = this.datas[silbilingIndex];
        this.level.parseDataAddPeople(data);
        this.level.gamePauseFlag = false;
        this.completeFun && this.completeFun();
        this.node.destroy();
        this.node.removeFromParent();
    }

}
